package com.shinewind.sso.filter;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.home.service.IUserInfoMappingService;
import com.shinewind.info.service.IUserDetailService;
import com.shinewind.info.service.IUserInfoService;
import com.shinewind.info.service.IUserRoleService;
import com.shinewind.pojo.entity.UserDetail;
import com.shinewind.pojo.entity.UserInfo;
import com.shinewind.pojo.entity.UserInfoMapping;
import com.shinewind.pojo.entity.UserRole;
import com.shinewind.pojo.result.Result;
import com.shinewind.pojo.dto.MyUserDto;
import com.shinewind.sso.entity.MyUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MySmSCodeFilter extends OncePerRequestFilter {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    protected void doFilterInternal(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //请求路径中时是否包含register关键字，并且请求必须是post
        if(StringUtils.contains(req.getRequestURI(),"/register") && StringUtils.equalsIgnoreCase(req.getMethod(),"post")){
            MyUserDto userDto = new ObjectMapper().readValue(req.getInputStream(), MyUserDto.class);
            System.out.println("smsCaptCha:"+userDto.getVerify());

            if(redisTemplate.hasKey(userDto.getTelePhone())){
                System.out.println("redisSmsCaptCha:"+redisTemplate.opsForValue().get(userDto.getTelePhone()));
                //验证码与输入的相同
                if(userDto.getVerify().equals(redisTemplate.opsForValue().get(userDto.getTelePhone()))){
                    req.setAttribute("userDto",userDto);
                    chain.doFilter(req,resp);
                }
                //验证码匹配失败
                else{
                    resp.setContentType("application/json;charset=utf-8");
                    resp.getWriter().write(new ObjectMapper().writeValueAsString(Result.fail(false, messageConstant.VERIFY_ERROR)));
                    resp.getWriter().flush();
                    resp.getWriter().close();
                }
            }
            //验证码过期
            else{
                resp.setContentType("application/json;charset=utf-8");
                resp.getWriter().write(new ObjectMapper().writeValueAsString(Result.fail(false, messageConstant.VERIFY_TIMEOUT)));
                resp.getWriter().flush();
                resp.getWriter().close();
            }
        }
        else{
            chain.doFilter(req,resp);
        }
    }
}
